if not pattern then pattern = {} end

if not pattern.move then

pattern.move = {last_time = {}}

if not ScriptsRootPath then
	ScriptsRootPath = getScriptPath() .. "\\..\\"
end

pattern.move.patterns_init = lib.path.init .. "patterns.ini"

dofile (ScriptsRootPath .. "lib\\lib.lua")

function pattern.move.Init()
	pattern.move.params = lib:LoadIni(pattern.move.patterns_init, "move",
		{	trend = 1.5,
			period = 30,
			notify_time = "10:00",
			notify_period = 60
		}
	)
	if pattern.move.params.notify_time then
		pattern.move.params.notify_time = split(pattern.move.params.notify_time, ":")
	end
	if processor and processor.log then
		processor:log("pattern.move.params", pattern.move.params)
	end
	--message("pattern.move.params", lib:toline(pattern.move.params))
end

function pattern.move.OnCandle(source, index, data)
	if not source then return end
	if not index then index = 1 end
	if not pattern.move.params then pattern.move.Init() end
	--[ [
	if pattern.move.params.notify_time and pattern.move.trade_date ~= lib:FormatDate() then
		pattern.move.notify_time = lib:getTradeDate()
		pattern.move.trade_date = lib:FormatDate()
		pattern.move.notify_time.hour = pattern.move.params.notify_time[1] or 0
		pattern.move.notify_time.min = pattern.move.params.notify_time[2] or 0
		pattern.move.notify_time.sec = pattern.move.params.notify_time[3] or 0
		pattern.move.notify_time.ms = 0
		pattern.move.notify_time.mcs = 0
		
		pattern.move.notify_time.start = lib:tickseconds(pattern.move.notify_time)
		pattern.move.notify_time.stop = pattern.move.notify_time.start
			+ 60 * (pattern.move.params.notify_period or 24 * 60)
	end
	--]]
	local candle0 = lib:GetLastCandle(source, index)
	local candle1 = lib:GetLastCandle(source, index + 1)
	if candle0 and candle1 then
		local datetime = lib:tickseconds(candle0.datetime)
		local datetime1 = lib:tickseconds(candle1.datetime)
		local sec = datetime - datetime1
		local period = math.ceil(pattern.move.params.period * 60 / sec)
		if period <= 0 then period = 1 end
		
		local pmin, pmax, is_long = lib:GetCandleRange(source, index, period)
		if pmin then
			local percent = round((pmax - pmin) * 100 / pmin, 2)
			
			if percent >= pattern.move.params.trend then
				if not pattern.move.last_time[candle0.instrument.code] then
					pattern.move.last_time[candle0.instrument.code] = {}
				end
				
				pattern.move.last_time[candle0.instrument.code][datetime] = is_long
				
				if pattern.move.last_time[candle0.instrument.code][datetime1] ~= is_long
				then
					local price
				
					if is_long then
						price = pmax
					else
						price = pmin
						percent = -percent
					end
										
					datetime = candle0.datetime
					datetime.is_long__ = is_long
					
					datetime.hint__ = string.format("MOVE %s %s%%",
						lib:FormatPrice(price, candle0.instrument),
						--lib:bool2side(is_long),
						tostring(percent)
					)
					if not pattern.move.notify_time
						or pattern.move.notify_time.start <= lib:tickseconds(datetime)
						and pattern.move.notify_time.stop > lib:tickseconds(datetime)
					then
						datetime.notify__ = candle0.instrument.code .. " " .. datetime.hint__
					end
					lib:AddLine(lib.path.log .. "MOVE." .. candle0.instrument.code .. "." .. lib:FormatDate() .. ".log", lib:FormatTime(datetime))

					return price, datetime
				end
			end
		end
	end
end

end -- of file
